from sqlalchemy import Column, String, Text, DateTime, ForeignKey, Enum as SQLEnum, Integer
from sqlalchemy.sql import func
from app.models.database import Base
import enum

class KnowledgeBaseVisibility(str, enum.Enum):
    """知识库可见性"""
    PUBLIC = "PUBLIC"  # 公开
    PRIVATE = "PRIVATE"  # 私有
    DEPARTMENT = "DEPARTMENT"  # 部门

class KnowledgeBase(Base):
    """知识库模型"""
    __tablename__ = "knowledge_bases"

    id = Column(String(36), primary_key=True, index=True)
    name = Column(String(100), nullable=False, index=True)
    description = Column(Text)
    
    # 所有者
    owner_id = Column(String(36), ForeignKey('users.id', ondelete='CASCADE'), nullable=False)
    owner_name = Column(String(100))
    
    # 可见性
    visibility = Column(SQLEnum(KnowledgeBaseVisibility), default=KnowledgeBaseVisibility.PRIVATE, nullable=False)
    department = Column(String(100))  # 当visibility=DEPARTMENT时使用
    
    # 向量库配置
    vector_store_path = Column(String(512))  # FAISS索引路径
    embedding_model = Column(String(100))  # 使用的embedding模型
    
    # 统计
    document_count = Column(Integer, default=0)
    
    # 时间
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    def __repr__(self):
        return f"<KnowledgeBase(name='{self.name}', owner='{self.owner_name}')>"
